/******************************************************************************
COPYRIGHT 2012   : ATECH
Project          : 
Source File Name : lin1Nwn.c
Group            : SoftWare Team
Author           : 
Date First Issued: 12/18/2012                                                  
******************************************************************************/
#define lin1Nwn_c

/* #include */
#include "Slave_lin1_cfg.h"
#include "Slave_lin1_apis.h"
#include "Slave_lin1_appl.h"
#include "Slave_lin1_ldf.h"
#include "lin1Nwn.h"
//#include "sys_sleep.h"
#include "hwa_outputs.h"

/* #define */


/* static variables */
LIN1_tenNwmState LIN1_enNwmState = LIN1_nenNWM_PowerOn;
LIN1_tstNwmPara  LIN1_stNwmPara;


/*****************************************************************************/
/**
 * Function Name: void lin1_nwm_init(void)
 * Description:   netWork management initialization
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
void lin1_nwm_init(void)
{
    (void)memset(&LIN1_stNwmPara, 0x00, sizeof(LIN1_stNwmPara));
	LIN1_stNwmPara.u16BusInActivityQuickTime = LIN1_NWM_SLAVE_NODE_AUTO_SLEEP_TIME;
}

/*****************************************************************************
 * Function Name: void lin1_hook_vBusActivity(void)
 * Description:   
 * Bus inactivity is defined as no transitions between recessive and dominant
 * bit values1. Bus activity is the inverse. 
 *
 * Param:   none
 * Return:  none
 * Author:  
 *****************************************************************************/
void lin1_hook_vBusActivity(void)
{
    LIN1_stNwmPara.u16BusInActivityTime = 0;    
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetSleepCommandSleepCondition(void)
 * Description:   
 * The master sets the cluster into bus sleep mode 
 * by transmitting a go to sleep command.
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetSleepCommandSleepCondition(void)
{
//    if(lin1_sys_boGetEvent(LIN1_EVENT_RecievedSleepCommand))
//    {
//        /* this function can only be called once */
//        lin1_sys_vClearEvent(LIN1_EVENT_RecievedSleepCommand);
//        return TRUE;    
//    }
//    else
//    {
        return FALSE;    
//    }
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetBusInactivitySleepCondition(void)
 * Description:   
 * The slave node shall automatically enter bus sleep mode 
 * earliest 4 s and latest 10 s of bus inactivity.
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetBusInactivitySleepCondition(void)
{
//    if(LIN1_stNwmPara.u16BusInActivityTime == LIN1_stNwmPara.u16BusInActivityQuickTime)
//    {
//        return TRUE;    
//    }
//    else
//    {
        return FALSE;   
//    }
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetReserveSleepCondition(void)
 * Description:   
 * some reserve sleep condition if need 
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetReserveSleepCondition(void)
{
/*    if( HWA_GetInput(SI_IGN_IN)
     || HWA_RFGetCmd(RF_CMD_LOCK)
     || HWA_RFGetCmd(RF_CMD_UNLOCK))   
    {
        return FALSE;    
    }
    else
    {
        return TRUE;   
    }*/
    return FALSE;
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetSleepCondition(void)
 * Description:   sleep condition before lin sleep
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetSleepCondition(void)
{
//    if(lin1_nwm_boGetReserveSleepCondition())
//    {
//        if( lin1_nwm_boGetSleepCommandSleepCondition()
//         || lin1_nwm_boGetBusInactivitySleepCondition())
//        {   
//            /* any sleep,UART->EXIT */
//            lin1_ifc_UART_switchTo_EXTI();
//            return TRUE;    
//        }
//        else
//        {
            return FALSE;    
//        }        
//    }    
//    else
//    {
//        return FALSE;   
//    }    
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetGlobalWakeUpConditon(void)
 * Description:   add global wakeup conditions in this function 
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetGlobalWakeUpConditon(void)
{
//#ifdef LIN1_GLOBAL_WAKEUP_ITSELF
//    if(HWA_GetInput(SI_IGN_IN))
//    {
        return TRUE;
//    }
//    else
//    {
//        return FALSE;
//    }
//#else
//   return FALSE; 
//#endif
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetLocalWakeUpConditon(void)
 * Description:   add local wakeup conditions in this function
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetLocalWakeUpConditon(void)
{
//#ifdef LIN1_LOCAL_WAKEUP_ITSELF
//     if( HWA_RFGetCmd(RF_CMD_LOCK)
//	  || HWA_RFGetCmd(RF_CMD_UNLOCK)
//	 )
//     {
        return TRUE;   
//     }
//     else
//     {
//        return FALSE;   
//     }
//#else
//    return FALSE; 
//#endif
}

/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetRemoteWakeUpConditon(void)
 * Description:   add remote wakeup conditions in this function
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetRemoteWakeUpConditon(void)
{
#ifdef LIN1_REMOTE_WAKEUP_ITSELF
     if(lin1_sys_boGetEvent(LIN1_EVENT_RecievedRemoteWakeUp))
     {
        /* this function can only be called once */
        lin1_sys_vClearEvent(LIN1_EVENT_RecievedRemoteWakeUp);
        return TRUE;   
     }
     else
     {
        return FALSE;   
     }
#else
    return FALSE; 
#endif
}


/*****************************************************************************/
/**
 * Function Name: BOOL lin1_nwm_boGetWakeUpCondition(void)
 * Description:   wakeup condition after lin sleep
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL lin1_nwm_boGetWakeUpCondition(void)
{
    if( lin1_nwm_boGetGlobalWakeUpConditon()
     || lin1_nwm_boGetLocalWakeUpConditon()
     || lin1_nwm_boGetRemoteWakeUpConditon())
    {   /* any wakeup,EXIT->UART */
    #ifdef LIN1_REMOTE_WAKEUP_ITSELF
        lin1_ifc_EXTI_switchTo_UART();
    #endif
        return TRUE;      
    }
    else
    {
        return FALSE;
    }
}

/*****************************************************************************/
/**
 * Function Name: void lin1_nwm_vStateMachine1(void)
 * Description:   management for wakeup and sleep
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
void lin1_nwm_vStateMachine1(void)
{                       
    switch (LIN1_enNwmState)
    {
        case LIN1_nenNWM_PowerOn:              
             LIN1_enNwmState = LIN1_nenNWM_WakeUp1; 
        break;
        
        case LIN1_nenNWM_WakeUp1:
             if(lin1_nwm_boGetSleepCondition())
             {
                LIN1_enNwmState = LIN1_nenNWM_Sleep;
             }
             else
             {
                /* wakeup always here */
                if(LIN1_stNwmPara.u16BusInActivityTime < LIN1_stNwmPara.u16BusInActivityQuickTime)
                   LIN1_stNwmPara.u16BusInActivityTime++;         
             }
        break;
        
        case LIN1_nenNWM_WakeUp2:      
            if(LIN1_stNwmPara.u16WakeUpIntervalTime > 0) LIN1_stNwmPara.u16WakeUpIntervalTime--;
            if(LIN1_stNwmPara.u16WakeUpIntervalTime == 0) 
            {
                if(LIN1_stNwmPara.u8WakeUpTimes == LIN1_NWM_RETRY_WAKEUP_TIMES)
                {
                    LIN1_enNwmState = LIN1_nenNWM_WakeUp1;    
                }
                else
                {
	                /* wakeup again */
	                lin1_ifc_wake_Up();
	                LIN1_stNwmPara.u8WakeUpTimes++;
                    LIN1_stNwmPara.u16WakeUpIntervalTime = LIN1_NWM_AFTER_WAKEUP_WAIT_BREAK_TIME;  
                }                  
            }
            else
            {
                /* detected frame head */
                if(lin1_sys_boGetEvent(LIN1_EVENT_RecievedFrameHead))
                {
                    LIN1_enNwmState = LIN1_nenNWM_WakeUp1;    
                }
            }
        break;
             
        case LIN1_nenNWM_Sleep:
             if(lin1_nwm_boGetWakeUpCondition())
             {
                 if( lin1_nwm_boGetGlobalWakeUpConditon()
                  || lin1_nwm_boGetLocalWakeUpConditon())
                 {
                    /* need to wake up master node and other slave nodes */
                    lin1_ifc_wake_Up();
                    LIN1_stNwmPara.u8WakeUpTimes = 1;
                    LIN1_stNwmPara.u16WakeUpIntervalTime = LIN1_NWM_AFTER_WAKEUP_WAIT_BREAK_TIME;
                    lin1_sys_vClearEvent(LIN1_EVENT_RecievedFrameHead);
                    LIN1_enNwmState = LIN1_nenNWM_WakeUp2;
                 }
                 else
                 {  
                    /* remote wake up by master node or one slave node, other nodes also by remote wakeup */
                    LIN1_enNwmState = LIN1_nenNWM_WakeUp1; 
                 }                                                            
             }
             else
             {
                /* sleep always here */  
             }         
         break;
    }
}

/*****************************************************************************/
/**
 * Function Name: void lin1_nwm_vQuickSleep(void)
 * Description:   reduce 4s timer
 *
 * Param:   none	
 * Return:  TRUE/FALSE
 * Author:  AF
 * *****************************************************************************/
void lin1_nwm_vQuickSleep(void)
{
    LIN1_stNwmPara.u16BusInActivityQuickTime = LIN1_NWM_SLAVE_NODE_QUICK_SLEEP_TIME;    
}
             
/*****************************************************************************/
/**
 * Function Name: BOOL Comm_lin1_boGetSleepPermit(void)
 * Description:   
 *
 * Param:   none
 * Return:  none
 * Author: 
 *****************************************************************************/
BOOL Comm_lin1_boGetSleepPermit(void)
{
    if(LIN1_enNwmState == LIN1_nenNWM_Sleep)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/***********************end of lin1Nwn.c***********************************/

/******************************************************************************
 Revision Notes:

For each change to this file, be sure to record:
1. Who made the change and when the change was made.
2. Why the change was made and the intended result.

  Date     By     Reason For Change
mm/dd/yy  XXX     -----------------

******************************************************************************/